package com.qiantu.phone.ui.activity;
import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLSourceHomepageDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.SourceHomepageBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.RoomEnviromentAdapter;

import java.util.List;

import okhttp3.Call;

public class RoomEnviromentActivity extends AppActivity {
    private String roomSerialNo;
    private RecyclerView enviromentRecylerView;
    private RoomEnviromentAdapter mRoomEnviromentAdapter;
    private List<DeviceBean> deviceBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.room_enviroment_activity;
    }

    @Override
    protected void initView() {
        roomSerialNo = getIntent().getStringExtra("roomSerialNo");
        enviromentRecylerView = findViewById(R.id.enviroment_list);
        enviromentRecylerView.setLayoutManager(new LinearLayoutManager(this));
        mRoomEnviromentAdapter = new RoomEnviromentAdapter(this);
        enviromentRecylerView.setAdapter(mRoomEnviromentAdapter);
        mRoomEnviromentAdapter.setOnChildClickListener(R.id.btn_homepage_show_switch,new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                changeIsHomepage(position,((SwitchButton) childView).isChecked());
            }
        });
        mRoomEnviromentAdapter.setOnChildClickListener(R.id.btn_data_source,new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                gotoSourceDataView(position);
            }
        });
    }

    @Override
    protected void initData() {
        deviceBeans = LLDeviceDBManager.getInstance(getContext()).getEnviromentDevicesByRoomSerialNos(roomSerialNo);
        List<SourceHomepageBean> homepageBeans = LLSourceHomepageDBManager.getInstance(getContext()).getDataByRoomSerialNo(roomSerialNo);
        for(int i = 0;i < deviceBeans.size();i++){
            DeviceBean deviceBean = deviceBeans.get(i);
            for(int j = 0;j < homepageBeans.size();j++){
                SourceHomepageBean homepageBean = homepageBeans.get(j);
                if(Integer.valueOf(homepageBean.getAreaShowType()) == deviceBean.getEnvironmentType()){
                    deviceBean.setHomePage(homepageBean);
                    break;
                }
            }
        }
        mRoomEnviromentAdapter.setData(deviceBeans);
    }

    private void changeIsHomepage(int position,boolean isHomepage){
        DeviceBean deviceBean = deviceBeans.get(position);
        SourceHomepageBean homepageBean = deviceBean.getHomepage();
        if(homepageBean == null){

            toast(R.string.enviroment_unset_hit);
        }else{
            LLHttpManager.setRoomEnvironemntIsOn(this, homepageBean.getSerialNo(), isHomepage, new HttpCallback<HttpData<Void>>(null){
                @Override
                public void onStart(Call call) {
                    showDialog();
                }

                @Override
                public void onSucceed(HttpData<Void> result) {
                    super.onSucceed(result);
                    LLSourceHomepageDBManager.getInstance(getContext()).changeIsHomepage(homepageBean.getSerialNo(),isHomepage);
                    toast(R.string.set_success);
                }

                @Override
                public void onFail(Exception e) {
                }

                @Override
                public void onEnd(Call call) {
                    hideDialog();
                }
            });
        }
    }

    private void gotoSourceDataView(int position){
        DeviceBean deviceBean = deviceBeans.get(position);
        Intent intent = new Intent(this, RoomEnvironmentDataSourceAcitvity.class);
        intent.putExtra("room_serial_no", roomSerialNo);
        intent.putExtra("environment_type", deviceBean.getEnvironmentType());
        SourceHomepageBean homepageBean = deviceBean.getHomepage();
        if(homepageBean != null){
            intent.putExtra("device_serial_no", homepageBean.getDeviceSerialNo());
            intent.putExtra("area_serial_no", homepageBean.getSerialNo());
        }
        startActivityForResult(intent, new OnActivityCallback() {
            @Override
            public void onActivityResult(int resultCode, @Nullable Intent data) {
                if(resultCode == RESULT_OK){
                    initData();
                }
            }
        });
    }
}
